﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace GPM
{
    public partial class FrmMenu : BaseForm
    {
        public GridCheckMarksSelection selection;

        public FrmMenu()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void FrmMenu_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvMenu);
            LoadImages();
            RefreshMenu();
            
        }

        private void LoadImages() {
            for (int i = 0; i < imageListSmall.Images.Count; i++)
                repImageCombo.Items.Add(new ImageComboBoxItem(i, i));
        }

        public void RefreshMenu()
        {
            MenuController uc = new MenuController();
            WebpartController wc = new WebpartController();
            IList<CP.Entities.Menu> menus = uc.GetAllMenus();
            this.lookupeditParentID.DataSource = uc.GetAllMenuParents();
            this.repLookupEditWebpartID.DataSource = wc.GetAllWebparts();
            //selection.ClearSelection();
            this.gcMenu.DataSource = menus;
        }

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshMenu();
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (0 == selection.SelectedCount)
            {
                ExMessageBox.Show("提示信息：请选中要删除的记录！", AppSetter.SoftName);
                return;
            }

            if (ExMessageBox.Confirm("提示信息：确定要删除选中的记录吗？", AppSetter.SoftName) == DialogResult.No)
                return;

            IList<int> menuids = new List<int>();
            foreach (object o in selection.Selection)
            {
                CP.Entities.Menu menu = o as CP.Entities.Menu;
                if (null != menu)
                    menuids.Add(menu.menu_id);
            }
            try
            {
                MenuController gc = new MenuController();
                gc.DeleteMenus(menuids);
                LogHelper.Log("菜单管理", "删除", "", "menuids:" +
                   string.Join(",", menuids));
                ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
                RefreshMenu();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }

        }

        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmEditMenu frmedit = new FrmEditMenu();
            frmedit.ShowDialog(this);
        }

        private void barEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条记录编辑", AppSetter.SoftName);
                return;
            }
            CP.Entities.Menu info = selection.Selection[0] as CP.Entities.Menu;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }
            FrmEditMenu frmedit = new FrmEditMenu("1", info.menu_id);
            frmedit.ShowDialog(this);
        }

        private void gvMenu_DoubleClick(object sender, EventArgs e)
        {
            CP.Entities.Menu menu = gvMenu.GetFocusedRow() as CP.Entities.Menu;
            if (null == menu)
                return;

            FrmEditMenu frmedit = new FrmEditMenu("1", menu.menu_id);
            frmedit.ShowDialog(this);
        }
    }
}
